package com.glory.cqaospda.view

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.KeyEvent
import com.glory.cqaospda.R
import com.glory.cqaospda.base.Constant
import com.glory.cqaospda.base.MyApp
import com.glory.cqaospda.bean.EqpInfoBean
import com.glory.cqaospda.bean.LotInfoBean
import com.glory.cqaospda.viewmodel.SiteModel
import com.glory.mvvmnet.base.BaseActivity
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ActivityManager
import kotlinx.android.synthetic.main.activity_site.*
import kotlinx.android.synthetic.main.activity_site.tvDevice
import kotlinx.android.synthetic.main.activity_site.tvEAPstate
import kotlinx.android.synthetic.main.activity_site.tvHandle
import kotlinx.android.synthetic.main.activity_site.tvMESstate
import kotlinx.android.synthetic.main.activity_startjob.*
import kotlinx.android.synthetic.main.activity_title.*

class SiteActivity : BaseActivity<SiteModel>() {
    lateinit var lotsBean: LotInfoBean
    var action: String = ""
    var lotId: String = ""
    var stepName: String = ""
    var eqpId: String = ""
    var site: String = ""
    var handler: Handler = object : Handler() {
        @SuppressLint("HandlerLeak")
        override fun handleMessage(msg: Message) {
            when(msg.what) {
                0x10 -> {
                    var s: String = msg.obj as String
                    val intent = Intent()
                    intent.putExtra("type", "Error")
                    intent.putExtra("error", s)
                    val clazz = Class.forName("com.glory.cqaospda.view.SkipDialogActivity")
                    intent.setClass(this@SiteActivity, clazz)
                    startActivity(intent)
                }
            }
            super.handleMessage(msg)
        }
    }
    override fun initData() {

        viewModel.defUI.callObserve.observe(this@SiteActivity) {
            if (!Constant.okgoError) {
                when (it.type) {
                    1 -> {
                        lotsBean = it.item as LotInfoBean
                        tvDevice.text = lotsBean.mCustomerDevice
                        tvMarkingCode1.text = lotsBean.mMarkingCode1
                        tvMarkingCode2.text = lotsBean.mMarkingCode2
                        tvMarkingCode3.text = lotsBean.mMarkingCode3

                    }
                    2 -> {
                        val eqpBean = it.item as EqpInfoBean
                        tvHandle.text = eqpBean.EquipmentName
                        tvMESstate.text = eqpBean.EquipmentStatus
                        tvEAPstate.text = eqpBean.EquipmentStatus
                    }
                    3 -> {
                        intent.putExtra("type", "1")
                        intent.putExtra("istvBaseSubmit", "true")
                        intent.setClass(this, Startjob2Activity::class.java)
                        startActivity(intent)
                    }
                }
            }else{
                Constant.okgoError = false
            }
        }

        tvBaseSubmit.setOnClickListener {
            if (Constant.isFastClick()) {
                viewModel.defaultRunLotHis(stepName, action, lotsBean.mLotId, eqpId)
            }
        }

    }

    override fun initView(savedInstanceState: Bundle?) {

        FreeApi_ActivityManager.getActivityManager().addActivity(this)

        val instance = MyApp.instance
        instance!!.handler = handler
        tvUserName.text = Constant.UserName
        lotId = intent.getStringExtra("lotId").toString()
        action = intent.getStringExtra("action").toString()
        stepName = intent.getStringExtra("StepName").toString()
        eqpId = intent.getStringExtra("eqpId").toString()
        viewModel.defaultLotInfo(lotId, true)
        viewModel.defaultEqpInfo(eqpId)
        site = intent.getStringExtra("site").toString()
        tvTitle.text = site + "站点"
    }

    override fun layoutId(): Int {
        return R.layout.activity_site
    }

    @SuppressLint("SetTextI18n")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == 1) {
            when (resultCode) {
                108 -> {
                    finish()
                }
            }
        }
    }
    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            intent.putExtra("type", "KeyEvent")
            intent.setClass(this, SkipDialogActivity::class.java)
            startActivityForResult(intent, 1)
            return false
        }
        return super.onKeyUp(keyCode, event)
    }
}